﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LD.Host
{
    public partial class FrmCache : BaseFrm
    {
        public FrmCache()
        {
            InitializeComponent();
            this.Shown += FrmCache_Shown;
        }

        private void FrmCache_Shown(object sender, EventArgs e)
        {
            base.Permission(UserType.管理员);
            if (this.IsDisposed)
                return;

            Init();
        }
        private void Init()
        {
            DataTable dataType = GetDataType();
            this.类型.DataSource = dataType;
            this.类型.DisplayMember = "name";
            this.类型.ValueMember = "id";

            LoadData();
        }
        private void LoadData()
        {
            this.dgv_data.Rows.Clear();
            foreach (var item in Cache.Var)
            {
                this.dgv_data.Rows.Add(this.dgv_data.Rows.Count + 1,
                    item.Name,
                    item.Describe,
                    item.DataType,
                    item.Val,
                    "删除"
                    );
            }
        }
        private DataTable GetDataType()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("name");

            int index = 0;
            foreach (var item in Enum.GetValues(typeof(DataType)))
            {
                dt.Rows.Add(index, item);
                index++;
            }
            return dt;
        }

        private void dgv_data_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 5)
            {
                //删除
                this.dgv_data.Rows.RemoveAt(e.RowIndex);
            }
        }
        public void New()
        {
            int index = this.dgv_data.Rows.Add();

            this.dgv_data["序号", index].Value = this.dgv_data.Rows.Count;
            this.dgv_data["操作", index].Value = "删除";

            DataGridViewComboBoxCell cobCelll5 = this.dgv_data["类型", index] as DataGridViewComboBoxCell;
            cobCelll5.Value = (int)DataType.Int + "";
        }
        public void Save()
        {
            List<Variable> varList = new List<Variable>();
            foreach (DataGridViewRow item in this.dgv_data.Rows)
            {
                if (item.Cells["名称"].Value != null &&
                    item.Cells["类型"].Value != null &&
                    !string.IsNullOrEmpty(item.Cells["名称"].Value.ToString()) &&
                    !string.IsNullOrEmpty(item.Cells["类型"].Value.ToString()))
                {
                    Variable var = new Variable()
                    {
                        Name = item.Cells["名称"].Value.ToString(),
                        DataType = item.Cells["类型"].Value.ToString(),
                        Describe = item.Cells["注释"].Value == null ? "" : item.Cells["注释"].Value.ToString(),
                        Val = item.Cells["当前值"].Value == null ? "" : item.Cells["当前值"].Value.ToString()
                    };
                    var exist = varList.Where(p => p.Name == item.Cells["名称"].Value.ToString()).FirstOrDefault();
                    if (exist == null)
                        varList.Add(var);
                }
            }
            Cache.SetCache(CONST.VARIABLE, varList);
            Cache.SaveCache();
        }
        public void MRefresh()
        {
            LoadData();
        }
    }
}
